import React, { Component } from 'react';
import { Chart, Axis, Tooltip, Geom } from 'bizcharts';
import { Modal,Table } from 'antd';
import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
import ResizeObserver from 'resize-observer-polyfill';
import styles from './bar.less';

class PoliceBar extends Component {
  state = {
    height: 0,
    autoHideXLabels: false,
    modalByPolice: false,
    tableListDataByPolice: [
	    {name:"陈某", sex:"男",post:"主任科员", company_area:"北京", duty_level:"二级警长",police_rank:"二级警督"},
	    {name:"王某", sex:"男",post:"主任科员", company_area:"北京", duty_level:"二级警长",police_rank:"二级警督"},
	    {name:"李某", sex:"男",post:"主任科员", company_area:"北京", duty_level:"二级警长",police_rank:"二级警督"},
	    {name:"张某", sex:"男",post:"主任科员", company_area:"北京", duty_level:"二级警长",police_rank:"二级警督"},
	    {name:"姜某", sex:"男",post:"主任科员", company_area:"北京", duty_level:"二级警长",police_rank:"二级警督"},
	    {name:"赵某", sex:"男",post:"主任科员", company_area:"北京", duty_level:"二级警长",police_rank:"二级警督"},
	    {name:"钱某", sex:"男",post:"主任科员", company_area:"北京", duty_level:"二级警长",police_rank:"二级警督"},
	]
  };
  
  	columnsByPolice = [
				{
	      	title: '姓名',
	        dataIndex: 'name',
	        key: 'name',
        },  {
	        title: '性别',
	        dataIndex: 'sex',
	        key: 'sex',
        }, 	{
	        title: '行政区划',
	        dataIndex: 'company_area',
	        key: 'company_area',
        },  {
	        title: '职级',
	        dataIndex: 'duty_level',
	        key: 'duty_level',
        },  {
	        title: '职务',
	        dataIndex: 'post',
	        key: 'post',
        },  {
	        title: '拟授警衔',
	        dataIndex: 'police_rank',
	        key: 'police_rank',
        }
	];

  handleRoot = n => {
    this.root = n;
  };

  handleRef = n => {
    this.node = n;
  };

  resizeObserver() {
    const ro = new ResizeObserver(entries => {
      const { width, height } = entries[0].contentRect;
      this.resize();
      this.setState(preState => {
        if (preState.width !== width || preState.height !== height) {
          return {
            height,
          };
        }
        return null;
      });
    });
    if (this.root) {
      ro.observe(this.root);
    }
  }

  @Bind()
  @Debounce(400)
  resize() {
    if (!this.node) {
      return;
    }
    const canvasWidth = this.node.parentNode.clientWidth;
    const { data = [], autoLabel = true } = this.props;
    if (!autoLabel) {
      return;
    }
    const minWidth = data.length * 30;
    const { autoHideXLabels } = this.state;

    if (canvasWidth <= minWidth) {
      if (!autoHideXLabels) {
        this.setState({
          autoHideXLabels: true,
        });
      }
    } else if (autoHideXLabels) {
      this.setState({
        autoHideXLabels: false,
      });
    }
  }
	
  //警力弹窗
	handleModalByPolice = flag => {
		this.setState({
			modalByPolice: !!flag,
		});
	};
	
  render() {
    const {
      height: propsHeight,
      title,
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
      policeType,
    } = this.props;
    const { autoHideXLabels,modalByPolice,tableListDataByPolice } = this.state;

    const scale = {
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
    };

    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];
    const { height: stateHeight } = this.state;
    const height = propsHeight || stateHeight;
    return (
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
	     	<Modal
					width={800}
					centered
					destroyOnClose
					title="警力统计列表"
					visible={modalByPolice}
					onOk={()=> this.handleModalByPolice()}
					onCancel={()=> this.handleModalByPolice()}
					>
					<Table columns={this.columnsByPolice} dataSource={tableListDataByPolice}/>
				</Modal>
        <div ref={this.handleRef}>
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
          <Chart
          	onPlotClick={ev => {
					    var point = {
					      x: ev.x,
					      y: ev.y
					    };
					    if(tableListDataByPolice) {
					    	tableListDataByPolice.map((val,index)=> {
					    		if(ev.data) {
					    			this.setState({
								    	modalByPolice: true
								    })
					    			if(policeType == "company_area") {
					    				val.company_area = ev.data._origin.x	
					    			}else if(policeType == "duty_level") {
					    				val.duty_level = ev.data._origin.x	
					    			}else {
					    				val.police_rank = ev.data._origin.x	
					    			}
					    		}
					    		this.setState({
					    			tableListDataByPolice: [...tableListDataByPolice]
					    		})
					    	})
					    }
					  }}
            scale={scale}
            height={title ? height - 41 : height}
            forceFit={forceFit}
            data={data}
            padding={padding || 'auto'}
          >
            <Axis
              name="x"
              title={false}
              label={autoHideXLabels ? false : {}}
              tickLine={autoHideXLabels ? false : {}}
            />
            <Axis name="y" min={0} />
            <Tooltip showTitle={false} crosshairs={false} />
            <Geom type="interval" position="x*y" color={color} tooltip={tooltip} />
          </Chart>
        </div>
      </div>
    );
  }
}

export default PoliceBar;
